package top.likeqc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.likeqc.common.lang.Result;
import top.likeqc.dto.CourseDTO;
import top.likeqc.entity.Course;
import top.likeqc.entity.CoursePoint;
import top.likeqc.service.ICoursePointService;
import top.likeqc.service.ICourseService;

import java.util.List;

/**
 * <p>
 * 课程支撑（课程-指标点） 前端控制器
 * </p>
 *
 * @author likeqc
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/coursePoint")
public class CoursePointController extends BaseController {

    @Autowired
    ICoursePointService coursePointService;

    @Autowired
    ICourseService courseService;

    @GetMapping()
    public Result list(String key) {

        Page<CoursePoint> page = coursePointService.page(getPage(),
            new QueryWrapper<CoursePoint>().like(StringUtils.isNotBlank(key),
            "course_id", key));

        // 根据课程 id 获取课程信息
        page.getRecords().forEach(coursePoint -> {
            coursePoint.setCourse(courseService.getById(coursePoint.getCourseId()));
        });
        return Result.success(page);
    }

    @PostMapping()
    public Result edit(@RequestBody CoursePoint coursePoint) {
        boolean save = coursePointService.saveOrUpdate(coursePoint);
        if (save) {
            return Result.success();
        } else {
            return Result.fail("新增、修改课程目标，操作失败");
        }
    }

    @DeleteMapping()
    public Result delete(@RequestBody List<Integer> ids) {
        boolean b = coursePointService.removeByIds(ids);
        if (b) {
            return Result.success();
        } else {
            return Result.fail("删除年级，操作失败");
        }
    }


}
